#import "BWSymbolDoc.h"
#import "BWStitchEditController.h"
#import "BWSymbol.h"

@implementation BWSymbolDoc


- (id) init
{
    if (self = [super init]) {
        // any additional initialization
        symbols = [[NSMutableArray alloc] init];
        editors = [[NSMutableArray alloc] init];
    }

    return (self);

} // init


- (void) awakeFromNib
{
    NSTableColumn *column;

    column = [[tableView tableColumns] objectAtIndex: 0];
    [column setDataCell: 
                [[[NSImageCell alloc] init] autorelease]];
    [column setEditable: NO];
    
    column = [[tableView tableColumns] objectAtIndex: 1];
    [column setDataCell: 
                [[[NSTextFieldCell alloc] init] autorelease]];
    [column setEditable: NO];

    [tableView setTarget: self];
    [tableView setDoubleAction: @selector(openEditor:)];

} // awakeFromNib

- (void) dealloc
{
    [editors release];
    [symbols release];

} // dealloc


- (NSString *) windowNibName
{
    return (@"BWSymbolDoc");
} // windowNibName


- (void) editorChanged: (NSNotification *) notification
{
    if ([notification name] == BWStitchEditController_ChangeNotification) {
        [tableView reloadData];
    }

    if ([notification name] == BWStitchEditController_ClosedNotification) {
        [editors removeObject: [notification object]];
    }

} // editorChanged


- (void) registerNotifications: (BWStitchEditController *) editor
{
    NSNotificationCenter *center;
    center = [NSNotificationCenter defaultCenter];

    [center addObserver: self
            selector: @selector(editorChanged:)
            name: nil
            object: editor];

} // registerNotifications


- (IBAction) newStitch: (id) sender
{
    // figure out the new id
    int maxId = 0;

    {
        NSEnumerator *enumerator = [symbols objectEnumerator];
        BWSymbol *symbol;
        while (symbol = [enumerator nextObject]) {
            maxId = MAX (maxId, [symbol symbolId]);
        }
    }

    BWStitchEditController *editor;

    // create new empty BWSymbol
    // 
    BWSymbol *newPath;
    newPath = [[BWSymbol alloc] init];
    [newPath setName: @"bork"];
    [newPath setSymbolId: maxId + 1];

    [symbols addObject: newPath];

    editor = [[BWStitchEditController alloc] 
                 initWithRawPath: newPath];

    [editor showWindow: self];
    [editors addObject: editor];
    [self addWindowController: editor];
    [editor release];

    [self registerNotifications: editor];

    [tableView reloadData];
    
} // newStitch


- (IBAction) openEditor: (id) sender
{
    int row;
    row = [tableView selectedRow];

    if (row >= 0) {
        BWStitchEditController *editor;
        BWSymbol *path;
        BOOL found = NO;
        
        path = [symbols objectAtIndex: row];

        // bring an existing editor to the front
        int i;
        for (i = 0; i < [editors count]; i++) {
            editor = [editors objectAtIndex: i];
            if ([editor rawPath] == path) {
                [editor showWindow: nil];
                found = YES;
                break;
            }
        }

        // otherwise open a new editor
        if (!found) {
            
            editor = [[BWStitchEditController alloc]
                         initWithRawPath: path];
            [editor showWindow: self];
            [editors addObject: editor];
            [self addWindowController: editor];
            [editor release];
            [self registerNotifications: editor];
        }
    }

} // openEditor



//
// --------------------------------------------------
//


- (NSData *) dataRepresentationOfType: (NSString *) aType
{
    NSEnumerator *enumerator = [symbols objectEnumerator];
    BWSymbol *symbol;
    int i = 0;

    while (symbol = [enumerator nextObject]) {
        [symbol setPackageName: [self fileName]];
        [symbol setSymbolId: i];
        i++;
    }

    NSData *data;
    data = [NSArchiver archivedDataWithRootObject: symbols];

    return (data);

} // dataRepresentationOfType



- (BOOL) loadDataRepresentation: (NSData *) data 
                         ofType: (NSString *) aType
{
    [symbols release];
    symbols = [NSUnarchiver unarchiveObjectWithData: data];
    [symbols retain];

    return (YES);

} // loadDataRepresentation

/*
 * -----
 */

- (int) numberOfRowsInTableView: (NSTableView *) tableView
{
    return ([symbols count]);
} // numberOfRowsInTableView


- (NSImage *) makeRowImage: (BWSymbol *) rawPath
{
    NSImage *image;

    image = [[NSImage alloc] initWithSize: NSMakeSize(32.0, 32.0)];
    [image setFlipped: YES];
    [image lockFocus];

    NSBezierPath *path;
    path = [rawPath pathInRect: NSMakeRect(0.0, 0.0, 32.0, 32.0)];
    [[NSColor blueColor] set];
    [path fill];
    [image unlockFocus];

    return (image);

} // makeRowImage


- (id) tableView: (NSTableView *)theTableView 
       objectValueForTableColumn: (NSTableColumn *) tableColumn 
       row: (int) row
{
    id result;

    BWSymbol *symbol = [symbols objectAtIndex: row];
    
    if ([[tableView tableColumns] objectAtIndex: 1]
        == tableColumn) {
        result = [symbol name];

    } else if ([[tableView tableColumns] objectAtIndex: 2]
        == tableColumn) {
        result = [NSNumber numberWithInt: [symbol symbolId]];

    } else {
        result = [self makeRowImage: symbol];
    }
    
    return (result);
    
} // objectValueForTableColumn



@end // BWSymbolDoc


